import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sports_new/resources/colours.dart';
import 'package:sports_new/widgets/max_bottom_sheet.dart';

import '../../../server/api/user_api.dart';
import '../../../utils/toast_utils.dart';
import '../../../utils/user.dart';
import '../../../utils/utils.dart';
import '../../../widgets/base/qkd_button.dart';
import '../../../widgets/base/qkd_text.dart';

class TeamFocusButton extends StatelessWidget {
  const TeamFocusButton({
    super.key,
    required this.sportsId,
    required this.id,
    required this.isFocus,
    this.onTap,
    this.teamName
  });

  const TeamFocusButton.soccer({
    super.key,
    required this.id,
    required this.isFocus,
    this.onTap,
    this.teamName
  }):sportsId = 1;

  const TeamFocusButton.basket({
    super.key,
    required this.id,
    required this.isFocus,
    this.onTap,
    this.teamName
  }):sportsId = 2;

  final int sportsId;
  final int id;
  final String? teamName;
  final bool isFocus;
  final ValueChanged<bool>? onTap;

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.symmetric(vertical: 16),
      child: GestureDetector(
        onTap: () => User.needLogin(onFocus),
        child: Container(
          height: 24,
          width: 56,
          alignment: Alignment.center,
          decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(4),
              border: Border.all(
                  color: !isFocus
                      ? Colours.white
                      : Colours.white.withOpacity(0.6),
                  width: 0.5)),
          child: QkdText(
              !isFocus? '关注' : '已关注',
              needCenter: true,
              color: !isFocus
                  ? Colours.white
                  : Colours.white.withOpacity(0.6),
              fontSize: 13
          ),
        ),
      ),
    );
  }

  onFocus() async {
    int? result;
    if (isFocus) {
      result = await UserApi.unFocusTeam(id,sportsId);
      if (result == 200) {
        ToastUtils.showDismiss("取消关注成功");
        onTap?.call(false);
      } else {
        ToastUtils.showDismiss("取消关注失败");
      }
    } else {
      Utils.onEvent('sjpd_qdxq', params: {'sjpd_qdxq': '3'});
      result = await UserApi.focusTeam(id,sportsId);
      if (result == 200) {
        ToastUtils.showDismiss("关注成功");
        onTap?.call(true);
        Get.bottomSheet(sheet());
      }
    }
  }

  Widget sheet(){
    return MaxBottomSheet(
      onClosing: (){},
      shape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.vertical(top: Radius.circular(12))
      ),
      clipBehavior: Clip.antiAlias,
      builder: (context) => Padding(
        padding: const EdgeInsets.fromLTRB(24, 16, 24, 0),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            const QkdText("关注成功",fontWeight: FontWeight.w500,fontSize: 16),
            Container(height: 4),
            QkdText("已自动关注${teamName??"球队"}近期比赛",color: Colours.grey66),
            Container(height: 16),
            Image.asset(
              Utils.getImgPath("team_focus_hint.png"),
              fit: BoxFit.cover,
            ),
            Container(height: 16),
            QkdButton(
              width: 248,height: 40,
              onPressed: navigator?.pop,
              text: "知道了",
              borderRadius: 37.5,
              foregroundColor: Colours.white,
              backgroundColor: Colours.main,
              textStyle: const TextStyle(
                fontSize: 16,fontWeight: FontWeight.w500
              ),
            ),
            Container(height: 12)
          ],
        ),
      )
    );
  }
}
